// 修改为您的apikey.可在官网（https://www.yunpian.com)登录后获取
var http        = require('http');
var https       = require('https');
var qs          = require('querystring');
const CSystem   = require('./system/System');

//aliyun sms
const Core = require('@alicloud/pop-core');




var apikeyId_ali        = 'LTAI4FuSyScKpEvo1zS56Exr';
var apikeySecret_ali        = 'kmdFYES9C4wxoOpwxhKThYRxSHvzrt';
var signname_ali        = "全民互动";
var templatecode_ali        = "SMS_177550266";
var sms_host_ali        = 'https://dysmsapi.aliyuncs.com';


/* */
var client = new Core({
  accessKeyId: apikeyId_ali,
  accessKeySecret: apikeySecret_ali,
  endpoint: sms_host_ali,
  apiVersion: '2017-05-25'
});
//end of aliyun sms


//var text_tpl    = '【优德互动】您的验证码是#code。如非本人操作，请忽略本短信';

// 智能匹配模板发送https地址
var sms_host    = 'sms.yunpian.com';
//  发送短信网址
var send_sms_uri= '/v2/sms/single_send.json';
//
var apikey      = 'fee25ec73a4bb74a2db6eb792725bb75';
// 修改为您要发送的短信内容
var text_tpl    = '【全民互动】您的验证码是#code。如非本人操作，请忽略本短信';

class CSendMsg {
    //  构造
    constructor() {

    }
    //  初始化
    Init() {

    }
    GetChlSrc(account,res) {         
        //
        CSystem.GetAccountInfo().QuerySource(account,res);
 
    }
    SendSource(account, SourceInfo,jwcontext) {         
        //
        CSystem.GetAccountInfo().UpdateSource(account, SourceInfo, jwcontext);
 
    }

    //  发送验证码 修改为您要发送的手机号码, 电话号码用多个号码用逗号隔开
    SendCode(mobile, code) {
        //
        this.send_sms(send_sms_uri, apikey, mobile, code);
        //
        CSystem.GetAccountInfo().UpdateCheckCode(mobile, code);
    }
    //  发送验证码 修改为您要发送的手机号码, 电话号码用多个号码用逗号隔开
    SendCode2(id, mobile, code) {
        //
        this.send_sms(send_sms_uri, apikey, mobile, code);
        //
        CSystem.GetAccountInfo().UpdateCheckCode2(id, mobile, code);
    }    
    // 
    send_sms(uri, apikey, mobile, code) {
        var post_data = {
            'apikey': apikey,
            'mobile': mobile,
            'text': text_tpl.replace(/#code/, code),
        }; 
        //这是需要提交的数据  
        var content = qs.stringify(post_data);
		//如果使用aliyun sms，comment below line
        //this.post(uri, content, sms_host);
		
		//deal with aliyun sms
		var params = {
		  "RegionId": "cn-hangzhou",
		  "PhoneNumbers": mobile,
		  "SignName": signname_ali,
		  "TemplateCode": templatecode_ali,
		  "TemplateParam": "{\"code\":\"" + code + "\"}"
		}
		
		var requestOption = {
		  method: 'POST'
		};
		
		client.request('SendSms', params, requestOption).then((result) => {
		  console.log(JSON.stringify(result));
		}, (ex) => {
		  console.log(ex);
		})
		//end of aliyun sms
		
    }
    //
	postJsonUnirest(myurl,CHJInfo,callback) {
        var unirest = require("unirest");

        var req = unirest("POST", myurl);
        
        req.headers({
            "cache-control": "no-cache",
            "content-type": "application/json"
        });
        //CHJInfo=JSON.parse(CHJInfo);
        console.log("111111111"+CHJInfo);
        var content = JSON.stringify(CHJInfo);
        req.type("json");
        req.send(content);
        
        req.end(function (res) {
          if(callback){
            callback(res.body);
        }
          console.log(res.body);
        });
    }
    post(uri, content, host) {
        var options = {
            hostname: host,
            port: 443,
            path: uri,
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
            }
        };
        var req = https.request(options, function (res) {
            // console.log('STATUS: ' + res.statusCode);  
            // console.log('HEADERS: ' + JSON.stringify(res.headers));  
            res.setEncoding('utf8');
            res.on('data', function (chunk) {
                console.log('BODY: ' + chunk);
            });
        });
        //console.log(content);
        req.write(content);
    
        req.end();
    }
	
	
	
	
	
	
	
    postDatwwwww(uri, post_data, host ,callback) {
        var content = qs.stringify(post_data);
            //var content = JSON.parse(post_data);
            var options = {
                hostname: host,
                port: 80,
                path: uri+content,
                method: 'GET',
                headers: {
                   
                }
            };
            var req = http.request(options, function (res) {
                // console.log('STATUS: ' + res.statusCode);  
                // console.log('HEADERS: ' + JSON.stringify(res.headers));  
                res.setEncoding('utf8');
                res.on('data', function (chunk) {
    
                    console.log('BODY: ' + chunk);
                    
                    if(callback){
                        callback(chunk);
                    }
                });
            });
            //console.log(content);
            req.write(content);
        
            req.end();
        }
    postDataasa(uri, post_data, host ,callback) {
        var content = qs.stringify(post_data);
        var options = {
            hostname: host,
            port: 81,
            path: uri,
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
            }
        };
        var req = http.request(options, function (res) {
            // console.log('STATUS: ' + res.statusCode);  
            // console.log('HEADERS: ' + JSON.stringify(res.headers));  
            res.setEncoding('utf8');
            res.on('data', function (chunk) {

                console.log('BODY: ' + chunk);

                if(callback){
                    callback(chunk);
                }
            });
        });
        //console.log(content);
        req.write(content);
    
        req.end();
    }

    postDataas(uri, post_data, host ,callback) {
        var content = qs.stringify(post_data);
        var options = {
            hostname: host,
            port: 8080,
            path: uri,
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
            }
        };
        var req = http.request(options, function (res) {
            // console.log('STATUS: ' + res.statusCode);  
            // console.log('HEADERS: ' + JSON.stringify(res.headers));  
            res.setEncoding('utf8');
            res.on('data', function (chunk) {

                console.log('BODY: ' + chunk);

                if(callback){
                    callback(chunk);
                }
            });
        });
        //console.log(content);
        req.write(content);
    
        req.end();
    }


    postDatHttps(uri, post_data, host ,callback) {
        var content = qs.stringify(post_data);
        var options = {
            hostname: host,
            port: 443,
            path: uri,
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
                'cache-control': 'no-cache'
            }
        };
        var req = https.request(options, function (res) {
            res.setEncoding('utf8');
            let dd = '';
            res.on('data', function (chunk) {
                console.log('BODY: ' + chunk);
                dd = dd + chunk;
            });
            res.on('end', function () {
                if(callback){
                    callback(dd);
                }
            });
        });
        console.log(content);
        req.write(content);   
        req.end();
    }
    

    postDatg(uri, post_data, host ,callback) {
        var content = post_data;
        var options = {
            hostname: host,
            port: 80,
            path: uri,
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
            }
        };
        var req = http.request(options, function (res) {
            // console.log('STATUS: ' + res.statusCode);  
            // console.log('HEADERS: ' + JSON.stringify(res.headers));  
            res.setEncoding('utf8');
            res.on('data', function (chunk) {
                console.log('BODY: ' + chunk);
                if(callback){
                    callback(chunk);
                }
            });
        });
        console.log(content);
        req.write(content);
    
        req.end();
    }

    postDati(uri, post_data, host ,callback) {
        var content = post_data;
        var options = {
            hostname: host,
            port: 9090,
            path: uri,
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
            }
        };
        var req = http.request(options, function (res) {
            // console.log('STATUS: ' + res.statusCode);  
            // console.log('HEADERS: ' + JSON.stringify(res.headers));  
            res.setEncoding('utf8');
            res.on('data', function (chunk) {
                console.log('BODY: ' + chunk);
                if(callback){
                    callback(chunk);
                }
            });
        });
        console.log(content);
        req.write(content);
    
        req.end();
    }
    postDatXK(uri, post_data, host ,callback) {
        var content = qs.stringify(post_data);
            var options = {
                hostname: host,
                port: 12223,
                path: uri+content,
                method: 'GET',
                headers: {
                   
                }
            };
            var req = http.request(options, function (res) {
                res.setEncoding('utf8');
                res.on('data', function (chunk) {
    
                    console.log('BODY: ' + chunk);
                    
                    if(callback){
                        callback(chunk);
                    }
                });
            });
            req.write(content);
        
            req.end();
        }
        	
    postDatHttpsjson(uri, post_data, host ,callback) {
        var content = JSON.stringify(post_data);
        var options = {
            hostname: host,
            port: 443,
            path: uri,
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            }
        };
        var req = https.request(options, function (res) {
            res.setEncoding('utf8');
            let dd = '';
            res.on('data', function (chunk) {
                console.log('BODY: ' + chunk);
                dd = dd + chunk;
            });
            res.on('end', function () {
                if(callback){
                    callback(dd);
                }
            });
        });
        console.log(content);
        req.write(content);   
        req.end();
    }
    //
    postData(uri, post_data, host ,callback) {
        var content = qs.stringify(post_data);
        var options = {
            hostname: host,
            port: 80,
            path: uri,
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
            }
        };
        var req = http.request(options, function (res) {
            // console.log('STATUS: ' + res.statusCode);  
            // console.log('HEADERS: ' + JSON.stringify(res.headers));  
            res.setEncoding('utf8');
            res.on('data', function (chunk) {
                console.log('BODY: ' + chunk);
                if(callback){
                    callback(chunk);
                }
            });
        });
        //console.log(content);
        req.write(content);
    
        req.end();
    }
    postDataa(uri, post_data, host ,callback) {
        var content = qs.stringify(post_data);
        var options = {
            hostname: host,
            port: 2118,
            path: uri,
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
            }
        };
        var req = http.request(options, function (res) {
            // console.log('STATUS: ' + res.statusCode);  
            // console.log('HEADERS: ' + JSON.stringify(res.headers));  
            res.setEncoding('utf8');
            res.on('data', function (chunk) {
                console.log('BODY: ' + chunk);
                if(callback){
                    callback(chunk);
                }
            });
        });
        //console.log(content);
        req.write(content);
    
        req.end();
    }

    postDatt(uri, post_data, host ,callback) {
        var content = post_data;
        var options = {
            hostname: host,
            port: 29981,
            path: uri,
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
            }
        };
        var req = http.request(options, function (res) {
            // console.log('STATUS: ' + res.statusCode);  
            // console.log('HEADERS: ' + JSON.stringify(res.headers));  
            res.setEncoding('utf8');
            res.on('data', function (chunk) {
                console.log('BODY: ' + chunk);
                if(callback){
                    callback(chunk);
                }
            });
        });
        //console.log(content);
        req.write(content);
    
        req.end();
    }
    postDatv(uri, post_data, host ,callback) {
 
        //var content = JSON.parse(post_data);
        var options = {
            hostname: host,
            port: 80,
            path: uri,
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            }
        };
        var req = http.request(options, function (res) {
            // console.log('STATUS: ' + res.statusCode);  
            // console.log('HEADERS: ' + JSON.stringify(res.headers));  
            res.setEncoding('utf8');
            res.on('data', function (chunk) {

                console.log('BODY: ' + chunk);

                if(callback){
                    callback(chunk);
                }
            });
        });
        //console.log(content);
        req.write(post_data);
    
        req.end();
    }
   postDatw(uri, post_data, host ,callback) {
    var content = qs.stringify(post_data);
        //var content = JSON.parse(post_data);
        var options = {
            hostname: host,
            port: 80,
            path: uri,
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
            }
        };
        var req = http.request(options, function (res) {
            // console.log('STATUS: ' + res.statusCode);  
            // console.log('HEADERS: ' + JSON.stringify(res.headers));  
            res.setEncoding('utf8');
            res.on('data', function (chunk) {

                console.log('BODY: ' + chunk);
                
                if(callback){
                    callback(chunk);
                }
            });
        });
        //console.log(content);
        req.write(content);
    
        req.end();
    }

    //add by wuhao
    postDatKouDai(uri, post_data, host ,callback) {
 
//       var content = JSON.parse(post_data);
        var content = JSON.stringify(post_data);
        var options = {
            hostname: host,
            port: 80,
            path: uri,
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            }
        };
        var req = http.request(options, function (res) {
            // console.log('STATUS: ' + res.statusCode);  
            // console.log('HEADERS: ' + JSON.stringify(res.headers));  
            res.setEncoding('utf8');
            res.on('data', function (chunk) {

                console.log('BODY: ' + chunk);

                if(callback){
                    callback(chunk);
                }
            });
        });
        //console.log(content);
        req.write(content);
    
        req.end();
    }
           //end by wuhao

	  postDatKouDai1(uri, post_data,Id, Sign,host ,callback) {
        console.log('请求参数:::'+post_data);
        var params  = JSON.stringify(post_data);
        console.log('请求参数:::'+params);
        var options = {
            hostname: host,
            port: 9300,
            path: uri,
            method: 'POST',
            headers: {
                "Content-Type": "application/json",
                "Authorization": Id +" "+Sign,
            }
        };
        var req = http.request(options, function (res) {
            res.setEncoding('utf8');
            res.on('data', function (chunk) {
                if(callback){
                    callback(chunk);
                }
            });
        });
        req.write(params);
    
        req.end();
    }
	

    postDatu(uri, post_data, host ,callback) {
        var content = post_data;
        var options = {
            hostname: host,
            port: 8080,
            path: uri,
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            }
        };
        var req = http.request(options, function (res) {
            // console.log('STATUS: ' + res.statusCode);  
            // console.log('HEADERS: ' + JSON.stringify(res.headers));  
            res.setEncoding('utf8');
            res.on('data', function (chunk) {
                console.log('BODY: ' + chunk);
                if(callback){
                    callback(chunk);
                }
            });
        });
        //console.log(content);
        req.write(content);
    
        req.end();
    }

    postDatUnirest(myurl,CHJInfo,callback) {
        var unirest = require("unirest");

        var req = unirest("POST", myurl);
        
        req.headers({
          "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
        });
        //CHJInfo=JSON.parse(CHJInfo);
        console.log("111111111"+CHJInfo);
        req.form(CHJInfo);
        
        req.end(function (res) {
          if (res.error) throw new Error(res.error);
          if(callback){
            callback(res.body);
        }
          console.log(res.body);
        });
    }
	
	
	postDatUnirest2(myurl,CHJInfo,callback) {
        var unirest = require("unirest");

        var req = unirest("POST", myurl);
        
        req.headers({
          "Content-Type": "application/x-www-form-urlencoded; charset=GBK"
        });
        //CHJInfo=JSON.parse(CHJInfo);
        console.log("111111111"+CHJInfo);
        req.form(CHJInfo);
        
        req.end(function (res) {
          if (res.error) throw new Error(res.error);
          if(callback){
            callback(res.body);
        }
          console.log(res.body);
        });
    }

//add by wuhao
    postDatDingDing(uri, post_data, host ,callback) {
 
//       var content = JSON.parse(post_data);
        var content = JSON.stringify(post_data);
        console.log("11111111"+content);
        var options = {
            hostname: host,
            port: 8080,
            path: uri,
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            }
        };
        var req = http.request(options, function (res) {
            // console.log('STATUS: ' + res.statusCode);  
            // console.log('HEADERS: ' + JSON.stringify(res.headers));  
            res.setEncoding('utf8');
            res.on('data', function (chunk) {

                console.log('BODY: ' + chunk);

                if(callback){
                    callback(chunk);
                }
            });
        });
        //console.log(content);
        req.write(content);
    
        req.end();
    }
           //end by wuhao





postDatHttps2(uri, post_data, host ,callback) {
        var content = qs.stringify(post_data);
        var options = {
            hostname: host,
            port: 443,
            path: uri,
            method: 'POST',
            strictSSL: false,
            rejectUnauthorized: false,
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
                'cache-control': 'no-cache'
            }
        };
        var req = https.request(options, function (res) {
            res.setEncoding('utf8');
            let dd = '';
            res.on('data', function (chunk) {
                console.log('BODY: ' + chunk);
                dd = dd + chunk;
            });
            res.on('end', function () {
                if(callback){
                    callback(dd);
                }
            });
        });
        console.log(content);
        req.write(content);   
        req.end();
    }
    postDatf(uri, post_data, host ,callback) {
        //var content = post_data;
        var content = qs.stringify(post_data);

        var options = {
            hostname: host,
            port: 9000,
            path: uri,
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' 
                //'Content-Type': 'application/json'
            }
        };
        var req = http.request(options, function (res) {
            // console.log('STATUS: ' + res.statusCode);  
            // console.log('HEADERS: ' + JSON.stringify(res.headers));  
            res.setEncoding('utf8');
            res.on('data', function (chunk) {
                console.log('BODY: ' + chunk);
                if(callback){
                    callback(chunk);
                }
            });
        });
        //console.log(content);
        req.write(content);
    
        req.end();
    }

}

//  全局变量
let InstCSendMsg = new CSendMsg();

//  导出
exports = module.exports = InstCSendMsg;